<?php

class CamposImagenesAdminController extends \BaseController {

	public function __construct(){
		
		$roles=array(
				'redactor'=>4,
				'admin'=>1,
				'usuario'=>'2',
				'admin.torneos'=>3
		);
		
		//permisos
		$rolesNoticias=array($roles['admin.torneos'],$roles['admin']);
		
		$this->beforeFilter('roles:'.implode($rolesNoticias,'-'));
	}
	
	
	/**
	 * Display a listing of noticiasadmins
	 *
	 * @return Response
	 */
	public function index()
	{
		$imagenesCampos = ImagenCampo::all();

		return View::make('contents.admin.campos-imagenes.index', compact('imagenesCampos'));
	}

	/**
	 * Show the form for creating a new noticiasadmin
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('contents.admin.campos-imagenes.create');
	}

	/**
	 * Store a newly created noticiasadmin in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), ImagenCampo::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$imagenCampo = new ImagenCampo();
		$imagenCampo->campo()->associate(Campo::find(Input::get('campo')));
		
		
		if(Input::hasFile('imagen')){
			
			$file            = Input::file('imagen');
			$destinationPath = public_path().'/assets/img/campos/';
			$filename        = str_random(6) . '_' . $file->getClientOriginalName();
			$uploadSuccess   = $file->move($destinationPath, $filename);
			
			$imagenCampo->url=$filename;
		}
		
		$imagenCampo->save();

		return Redirect::route('admin.campos-imagenes.index');
	}

	/**
	 * Display the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$imagenCampo = ImagenCampo::findOrFail($id);

		return View::make('contents.admin.campos-imagenes.show', compact('imagenCampo'));
	}

	/**
	 * Show the form for editing the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$imagenCampo = ImagenCampo::find($id);

		return View::make('contents.admin.campos-imagenes.edit', compact('imagenCampo'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$imagenCampo = ImagenCampo::findOrFail($id);

		$validator = Validator::make($data = Input::all(), ImagenCampo::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$imagenCampo->campo()->associate(Campo::find(Input::get('campo')));
		
		if(Input::hasFile('imagen')){
				
			$file            = Input::file('imagen');
			$destinationPath = public_path().'/assets/img/campos/';
			$filename        = str_random(6) . '_' . $file->getClientOriginalName();
			$uploadSuccess   = $file->move($destinationPath, $filename);
				
			File::delete($destinationPath.$imagenCampo->url);
			
			$imagenCampo->url=$filename;		
		}
		
		$imagenCampo->save();
		

		return Redirect::route('admin.campos-imagenes.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$imagenCampo = ImagenCampo::findOrFail($id);
		File::delete($destinationPath.$imagenCampo->url);
		ImagenCampo::destroy($id);

		return Redirect::route('admin.campos-imagenes.index');
	}

}